GM.StrikeMods={}

-- StrikeMod( string name, table clist )
-- name: The name that the gamemode uses to refer to this mod.
-- clist: A list of mods that should be deactivated while this is active.
-- bad1v1: (Optional) Whether or not to disable this when there are only two players playing.
function StrikeMod( name, clist, bad1v1 )
	local mod={}
	mod.name=name
	mod.clist=clist or {}
	mod.bad1v1=bad1v1 or false
	table.insert( GAMEMODE.StrikeMods, mod )
end

GM.MSWeapons = {}
GM.MSWeapons.Primary = {}
GM.MSWeapons.Secondary = {}

function MSWeapon( classname )
	local atype = weapons.Get(classname).Slot
	if atype==1 then
	table.insert( GAMEMODE.MSWeapons.Secondary, classname )
	else
	table.insert( GAMEMODE.MSWeapons.Primary, classname )
	end
end

function GM:InitPostEntity()
MsgN("==========================")
MsgN("Welcome to Metastrike ",self.Version,', the "',self.Edition,'" edition!') 

StrikeMod( "Movespeed Increased", {"Movespeed Decreased"})
StrikeMod( "Movespeed Decreased", {"Movespeed Increased"})
StrikeMod( "Max Health Increased", {"Max Health Decreased"})
StrikeMod( "Max Health Decreased", {"Max Health Increased"})
StrikeMod( "Super Jumping", {"No Jumping"})
StrikeMod( "No Jumping", {"Super Jumping"})
--StrikeMod( "Lightspeed Sprint", {"No Sprinting"})
StrikeMod( "No Sprinting", {"Lightspeed Sprint", "Attack While Sprinting"})
StrikeMod( "Gun Damage Increased", {"Gun Damage Decreased","No Guns"})
StrikeMod( "Gun Damage Decreased", {"Gun Damage Increased","No Guns"})
StrikeMod( "Rate of Fire Increased", {"Rate of Fire Decreased","No Guns"})
StrikeMod( "Rate of Fire Decreased", {"Rate of Fire Increased","No Guns"})
StrikeMod( "Accuracy Increased", {"Accuracy Decreased","No Guns"})
StrikeMod( "Accuracy Decreased", {"Accuracy Increased","No Guns"})
StrikeMod( "Knockback Bullets", {"No Guns"})
StrikeMod( "Laser Tracers", {"No Guns"})
StrikeMod( "Guns Steal Health", {"No Guns"})
StrikeMod( "50% Chance for No Damage", {"No Guns","Critical Hits"})
StrikeMod( "Incendiary Ammo", {"No Guns"})
StrikeMod( "No Ammo Drops", {"No Guns","More Ammo Dropped"})
StrikeMod( "Silent Guns", {"No Guns"})
StrikeMod( "Deadly Disease", {"AIDS"})
StrikeMod( "Attack While Sprinting", {"No Sprinting","No Guns","Crouch to Shoot"})
StrikeMod( "+25 Armor", {"+50 Armor","Full Armor"})
StrikeMod( "+50 Armor", {"+25 Armor","Full Armor"})
StrikeMod( "Full Armor", {"+50 Armor","+25 Armor"})
StrikeMod( "One Versus One", {"Swapped Player Models","Disguises", "MVP Versus MVP","Explode on Death"}, true)
StrikeMod( "Disguises", {"Swapped Player Models","One Versus One","MVP Versus MVP","No HUD","Wallhacks"}, true)
StrikeMod( "Gravity Guns", {})
StrikeMod( "Primary Weapons Only", {"Secondary Weapons Only","No Guns"})
StrikeMod( "Secondary Weapons Only", {"Primary Weapons Only","No Guns"})
StrikeMod( "No HUD", {"Disguises"})
StrikeMod( "No Guns", {"Gun Damage Increased","Gun Damage Decreased","Rate of Fire Increased","Rate of Fire Decreased","Accuracy Increased","Accuracy Decreased",
"Knockback Bullets","Laser Tracers","Guns Steal Health","50% Chance for No Damage","Incendiary Ammo","No Ammo Drops","Starting Ammo Decreased","Starting Ammo Increased",
"Silent Guns","Attack While Sprinting","Primary Weapons Only","Secondary Weapons Only","All Guns Have Scopes","Crouch to Shoot","Critical Hits","More Ammo Dropped"})
StrikeMod("Explode on Death",{"One Versus One","MVP Versus MVP"}, true)
StrikeMod("All Guns Have Scopes",{"No Guns"})
StrikeMod("Silent Footsteps",{})
StrikeMod("Crouch to Shoot", {"No Guns","Attack While Sprinting"})
StrikeMod("Wallhacks", {"Disguises"})
StrikeMod("Starting Ammo Increased", {"Starting Ammo Decreased", "No Guns"})
StrikeMod("Starting Ammo Decreased", {"Starting Ammo Increased", "No Guns"})
StrikeMod("Critical Hits", {"No Guns","50% Chance for No Damage"})
StrikeMod("More Ammo Dropped", {"No Guns", "No Ammo Drops"})
StrikeMod("MVP Versus MVP", {"Swapped Player Models","One Versus One", "Disguises"}, true)
StrikeMod("Healthbars", {})
StrikeMod("Instakill Fall Damage", {"No Fall Damage"})
StrikeMod("No Fall Damage", {"Instakill Fall Damage"})
StrikeMod("Swapped Player Models", {"Disguises","One Versus One","MVP Versus MVP"},true)
if math.random(100)==69 then StrikeMod("AIDS",{"Deadly Disease"}) MsgN("THERE IS NO CURE FOR AIDS") end

local barrels = ents.FindByModel("models/props_c17/oildrum001.mdl")
if barrels and #barrels>0 then
	StrikeMod("Explosive Barrels", {})
	print("Found barrels, adding Explosive Barrels mod...")
else
	print("No barrels on this map, disabling Explosive Barrels mod...")
end

MSWeapon("msw_pist_deagle")
MSWeapon("msw_pist_gl18")
MSWeapon("msw_rif_ak47")
MSWeapon("msw_rif_m4a1")
MSWeapon("msw_rif_famas")
MSWeapon("msw_snip_awp")
MSWeapon("msw_smg_ump45")
MSWeapon("msw_pist_ion")
MSWeapon("msw_pist_elites")
local cweps = hook.Call("AddCustomWeapons")
if cweps then
	self.CustomWeapons = true
	for k,v in pairs(cweps) do
		MSWeapon(v)
	end
end
end

function GM:DetermineRoundWeapons()
	GM = self
	if GM:MSCheckForMod("No Guns") then return false end
	local RWeaponPri = table.Random(GM.MSWeapons.Primary)
	local weapon1english = weapons.Get(RWeaponPri).PrintName
	local RWeaponSec = table.Random(GM.MSWeapons.Secondary)
	local weapon2english = weapons.Get(RWeaponSec).PrintName
	if GM:MSCheckForMod("Primary Weapons Only") then weapon2english="Nothing" end
	if GM:MSCheckForMod("Secondary Weapons Only") then weapon1english="Nothing" end
	--[[if GM:CheckForRule("gunsup") then
		weaponenglish="Everything!"
	end]]--
	PrintMessage( HUD_PRINTCENTER, "Round Weapons: \n"..weapon1english.."\n"..weapon2english )
	for k, v in pairs(player.GetAll()) do
		if v:Team() == TEAM_ADMIN or v:Team() == TEAM_MBAGS then
			if !GM:MSCheckForMod("Secondary Weapons Only") then v:Give(RWeaponPri) end
			if !GM:MSCheckForMod("Primary Weapons Only") then v:Give(RWeaponSec) end
			if GM:MSCheckForMod("Starting Ammo Decreased") then v:SetAmmo(v:GetAmmoCount("pistol")/2,"pistol") v:SetAmmo(v:GetAmmoCount("ar2")/2,"ar2") end
			if GM:MSCheckForMod("Starting Ammo Increased") then v:SetAmmo(v:GetAmmoCount("pistol")*2,"pistol") v:SetAmmo(v:GetAmmoCount("ar2")*2,"ar2") end
		end
		v:SendLua("surface.PlaySound('metastrike/misc/weapons_issue.wav')")
	end
end

function GM:DetermineRoundMods()
	self:PlaySoundToEverybody("metastrike/misc/rules_update.wav")
	GM = self
	--Init variables
	local MaxHP = 100
	local MoveSpd = 250
	local RunSpd = 370
	local JumpPwr = 200
	local Armor = 0
	
	local mods = table.Copy(GM.StrikeMods)
	local modnum = 0
	
	-- If there are only two players, disable pointless mods like 1v1.
	if #team.GetPlayers(TEAM_ADMIN)<2 or #team.GetPlayers(TEAM_MBAGS)<2 then
		local remlist={}
		for mk,mv in pairs(mods) do
			if mv.bad1v1 then table.insert(remlist, mods[mk]) end
		end
		for zk,zv in pairs(remlist) do
			if table.HasValue(mods, zv) then table.remove(mods, table.KeyFromValue(mods, zv)) end
		end
	end
	
	for i=1,3 do
		modnum = math.random(1,#mods)
		local dmod = mods[modnum].name
		SetGlobalString("ms_mod"..tostring(i), dmod)
		toremove = mods[modnum].clist
		table.remove(mods, modnum)
		if toremove then
			local remlist={}
			for rk,rv in pairs(toremove) do
				for mk,mv in pairs(mods) do
					if mv.name==rv then table.insert(remlist, mods[mk]) end
				end
			end
			for zk,zv in pairs(remlist) do
				if table.HasValue(mods, zv) then table.remove(mods, table.KeyFromValue(mods, zv)) end
			end
		end
	end
	
	--Change init variables
	if GM:MSCheckForMod("Max Health Increased") then
		MaxHP = 200
	end
	if GM:MSCheckForMod("Max Health Decreased") then
		MaxHP = 50
	end
	if GM:MSCheckForMod("Super Jumping") then
		JumpPwr = 500
	end
	if GM:MSCheckForMod("No Jumping") then
		JumpPwr = 0
	end
	if GM:MSCheckForMod("Movespeed Increased") then
		MoveSpd = 375
		RunSpd = 500
	end
	if GM:MSCheckForMod("Movespeed Decreased") then
		MoveSpd = 175
		RunSpd = 225
	end
	if GM:MSCheckForMod("Lightspeed Sprint") then
		RunSpd = 800  -- There. I left it in for doommarine23. Have fun.												(dumbass)
	end
	if GM:MSCheckForMod("No Sprinting") then
		RunSpd = MoveSpd
	end
	if GM:MSCheckForMod("+25 Armor") then
		Armor = 25
	end
	if GM:MSCheckForMod("+50 Armor") then
		Armor = 50
	end
	if GM:MSCheckForMod("Full Armor") then
		Armor = 100
	end
	
	-- Post-modup initialization
	for k,v in pairs(player.GetAll()) do
		v:SetHealth(MaxHP)
		v:SetMaxHealth(MaxHP)
		v:SetWalkSpeed(MoveSpd)
		v:SetRunSpeed(RunSpd)
		v:SetArmor(Armor)
		v:SetJumpPower(JumpPwr)
		if self:MSCheckForMod("Disguises") then
			v:SetModel("models/player/leet.mdl")
			v:SetPlayerColor( Vector( 0.31,0.53,0 ) )
		end
		if self:MSCheckForMod("Gravity Guns") then
			v:Give("weapon_physcannon")
		end
		if self:MSCheckForMod("Swapped Player Models") then
			if v:Team()==TEAM_MBAGS then v:SetModel("models/player/breen.mdl") v:SetPlayerColor(Vector(0,0,0)) end
			if v:Team()==TEAM_ADMIN then v:SetModel("models/player/kleiner.mdl") v:SetPlayerColor(Vector(1,1,1)) end
		end
	end
	if GM:MSCheckForMod("Explosive Barrels") then
		local barrels = ents.FindByModel("models/props_c17/oildrum001.mdl")
		for k,v in pairs(barrels) do
			local newbr = ents.Create("prop_physics")
			newbr:SetPos(v:GetPos())
			newbr:SetModel("models/props_c17/oildrum001_explosive.mdl")
			newbr:Spawn()
			v:Remove()
		end
	end
	if GM:MSCheckForMod("One Versus One") or GM:MSCheckForMod("MVP Versus MVP") then
		local adminTheOne = nil
		local mbagsTheOne = nil
		if GM:MSCheckForMod("One Versus One") then
			adminTheOne = table.Random(team.GetPlayers(TEAM_ADMIN))
			mbagsTheOne = table.Random(team.GetPlayers(TEAM_MBAGS))
		elseif GM:MSCheckForMod("MVP Versus MVP") then
			adminTheOne = table.Random(team.GetPlayers(TEAM_ADMIN))
			mbagsTheOne = table.Random(team.GetPlayers(TEAM_MBAGS))
			for _,pl in pairs(self:GetPlayingPlayers()) do
				if pl:Team()==TEAM_MBAGS and pl:Frags()>mbagsTheOne:Frags() then
					mbagsTheOne=pl
				elseif pl:Team()==TEAM_ADMIN and pl:Frags()>adminTheOne:Frags() then
					adminTheOne=pl
				end
			end
		end
		umsg.Start("MSNotice")
			umsg.String("PlayerSpecialAction")
			umsg.Entity(adminTheOne)
			umsg.String("is the Chosen Admin")
		umsg.End()
		umsg.Start("MSNotice")
			umsg.String("PlayerSpecialAction")
			umsg.Entity(mbagsTheOne)
			umsg.String("is the Chosen Mingebag")
		umsg.End()
		for _,pl in pairs(player.GetAll()) do
			if (pl:Team()==TEAM_MBAGS or pl:Team()==TEAM_ADMIN) and pl~=adminTheOne and pl~=mbagsTheOne then
				pl:KillSilent()
				pl:Spectate(OBS_MODE_CHASE)
				if pl:Team()==TEAM_ADMIN then pl:SpectateEntity(adminTheOne) else pl:SpectateEntity(mbagsTheOne) end
			end
		end
	end
	SetGlobalInt("ms_maxhp",MaxHP)
	SendUserMessage("updatemsmods")
end